using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using DTOLayer;

namespace DAOLayer
{
    public class TheLoaiSachDAO: AbstractDao
    {
        public int TestExisted(TheLoaiSachDTO dto)
        {
            int kq = -1;
            try
            {
                SqlConnection cn;
                cn = this.SqlConnectiondata();
                cn.Open();
                String str = "select count(*) from TheLoaiSach where TenTheLoai = ? and GhiChu = ?";
                SqlCommand cmd = new SqlCommand(str, cn);
                if ((int)cmd.ExecuteScalar() == 0)
                    kq = 1;
                cn.Close();
                return kq;
            }
            catch (Exception e)
            {
                return kq;
            }
            
        }
        public int InsertTheLoaiSach(TheLoaiSachDTO dto)
        {
            int kq = TestExisted(dto);
            if (kq == -1)
            {
                return -1;
            }
            try
            {
                SqlConnection cn;
                cn = this.SqlConnectiondata();
                cn.Open();
                String str = "insert into TheLoaiSach values (" + dto.MaTheLoai + ",'" + dto.TenTheLoai + "','" + dto.GhiChu + "')";
                
                SqlCommand cmd = new SqlCommand(str, cn);
                cmd.ExecuteNonQuery();
                cn.Close();
                return dto.MaTheLoai;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
        public int DeleteTheLoaiSach(int matheloai)
        {
            try
            {
                SqlConnection cn;
                cn = this.SqlConnectiondata();
                cn.Open();
                string str = "delete from TheLoaiSach where Matheloai = " + matheloai;
                SqlCommand cmd = new SqlCommand(str,cn);
                cmd.ExecuteNonQuery();
                cn.Close();
                return 1;
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        public int UpdateTheLoaiSach(TheLoaiSachDTO dto)
        {
            try
            {
                SqlConnection cn;
                cn = this.SqlConnectiondata();
                cn.Open();
                string str = "update TheLoaiSach set tentheloai = '" + dto.TenTheLoai + "' and ghichu = '" + dto.GhiChu + "' where matheloai = " + dto.MaTheLoai;
                SqlCommand cmd = new SqlCommand(str,cn);
                cmd.ExecuteNonQuery();
                cn.Close();
                return 1;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
        public DataTable LayDanhSach()
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection cn;
                cn = this.SqlConnectiondata();
                cn.Open();
                String str = "select * from TheLoaiSach";

                SqlDataAdapter d = new SqlDataAdapter(str, cn);
                d.Fill(dt);
                cn.Close();

            }
            catch (Exception e)
            {
                return null;
            }
            return dt;
        }
    }
}
